import { ref, computed, onMounted, onUnmounted } from 'vue'

import type {
  navItem,
  HomeType,
  HomeArticleParams,
  Article,
  DoctorParams,
  Doctor,
  LikeType
} from '@/types/home'

import { getHomeKnowledgeApi, getHomeDoctorApi, likeApi } from '@/services/home'

import { showFailToast, showConfirmDialog } from 'vant'

import { useWindowSize } from '@vueuse/core'

import { useConsultStore } from '@/stores'
import type { Router } from 'vue-router'
import type { ConsultType } from '@/enums'

export function homeNavs() {
  const homeConsultationNav = {
    class: '',
    span: '8',
    navs: [
      {
        label: {
          title: '问医生',
          desc: '按科室查问医生'
        },
        link: '/',
        icon: 'home-doctor',
        type: 0
      },
      {
        label: {
          title: '极速问诊',
          desc: '20s医生极速回复'
        },
        link: '/consult/fast',
        icon: 'home-graphic',
        type: 2
      },
      {
        label: {
          title: '开药门诊',
          desc: '线上买药更方便'
        },
        link: '/',
        icon: 'home-prescribe',
        type: 0
      }
    ]
  }

  const homeOrderNav = {
    class: 'nav',
    span: '6',
    navs: [
      {
        label: {
          title: '药品订单',
          desc: ''
        },
        link: '/',
        icon: 'home-order',
        type: 0
      },
      {
        label: {
          title: '健康档案',
          desc: ''
        },
        link: '/',
        icon: 'home-docs',
        type: 0
      },
      {
        label: {
          title: '我的处方',
          desc: ''
        },
        link: '/',
        icon: 'home-rp',
        type: 0
      },
      {
        label: {
          title: '疾病查询',
          desc: ''
        },
        link: '/',
        icon: 'home-find',
        type: 0
      }
    ]
  }

  const tabList: navItem[] = [
    {
      label: '关注',
      type: 'like'
    },
    {
      label: '推荐',
      type: 'recommend'
    },
    {
      label: '减脂',
      type: 'fatReduction'
    },
    {
      label: '饮食',
      type: 'food'
    }
  ]

  return {
    homeConsultationNav,
    homeOrderNav,
    tabList
    //文章列表数据和方法

    //医生列表数据和方法
  }
}

export function article(type?: HomeType | undefined) {
  const articlePageTotal = ref(0)

  const homeArticleParams = ref<HomeArticleParams>({
    current: 1,
    pageSize: 2,
    type: type || 'like'
  })

  const knowledgeList = ref<Array<Article>>([])

  const articleFinish = computed(
    () => articlePageTotal.value < homeArticleParams.value.current
  )

  const articleLoading = ref(false)

  async function getHomeKnowledge(getNewData: boolean = true) {
    if (getNewData) {
      homeArticleParams.value.current = 1
    }
    articleLoading.value = true
    try {
      const { data } = await getHomeKnowledgeApi(homeArticleParams.value)
      articlePageTotal.value = data.pageTotal
      if (getNewData) {
        knowledgeList.value = data.rows
      } else {
        knowledgeList.value = knowledgeList.value.concat(data.rows)
      }
      console.log(knowledgeList.value)

      if (!articleFinish.value) {
        homeArticleParams.value.current += 1
      }
    } catch (err) {
      showFailToast('加载失败')
    } finally {
      articleLoading.value = false
    }
  }

  return {
    homeArticleParams,
    articlePageTotal,
    knowledgeList,
    articleFinish,
    articleLoading,
    getHomeKnowledge
  }
}

export function doctor() {
  const doctorParams = ref<DoctorParams>({
    pageSize: 5,
    current: 1
  })
  const doctorList = ref<Doctor[]>([])

  async function getDoctorList(getNewData: boolean = true) {
    try {
      const { data } = await getHomeDoctorApi(doctorParams.value)
      if (getNewData) {
        doctorList.value = data.rows
      } else {
        doctorList.value = doctorList.value.concat(data.rows)
      }
      doctorList.value = data.rows
    } catch (error) {
      showFailToast('加载失败')
    }
  }

  onMounted(() => {
    getDoctorList()
  })

  return {
    doctorParams,
    doctorList,
    getDoctorList
  }
}

export function like(type: LikeType) {
  const likeLoading = ref(false)
  async function onLike(id: string) {
    likeLoading.value = true
    try {
      await likeApi({
        type,
        id
      })
      return true
    } catch (error) {
      showFailToast('关注失败')
      return Promise.reject()
    } finally {
      likeLoading.value = false
    }
  }
  return {
    likeLoading,
    onLike
  }
}

export function useStore(router: Router) {
  const store = useConsultStore()

  async function setStore(
    router: Router,
    link: string,
    path: string,
    type: ConsultType
  ) {
    try {
      await showConfirmDialog({
        title: '温馨提示',
        message: '是否使用已填写的数据'
      })
      router.push(link)
    } catch (error) {
      store.clear()
      setType(type)
      router.push(path)
    }
  }

  const setType = store.setType

  function onUseStore(path: string, type: ConsultType) {
    const { patientId, depId, illnessDesc, illnessTime, illnessType } =
      store.consult

    if (patientId) {
      setStore(router, '/consult/pay', path, type)
      return
    }
    if (illnessDesc && illnessTime && illnessType) {
      setStore(router, '/user/patient?isChange=1', path, type)
      return
    }

    if (depId) {
      setStore(router, '/consult/illness', path, type)
      return
    }

    setType(type)
    router.push(path)
  }

  return {
    setType,
    setStore,
    onUseStore
  }
}
